package com.amit.struts;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class DownlaodAction extends Action {
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		FileInputStream fis=null;
		OutputStream os=null;
		
		try
		{
			String path="D:/";
			String fileName="Java7.pdf";
			String filePath=path+fileName;
			response.setContentType("APPLICATION/OCTET-STREAM");
			String disHeader="Attachment;Filename="+fileName;
			response.setHeader("Content-Dispostion", disHeader);
			
			
			File file=new File(filePath);
			fis=new FileInputStream(file);
			os=response.getOutputStream();
			
			int i;
			
			while((i=fis.read())!=-1)
			{
				os.write(i);
			}
			
			os.flush();
		}
		catch (Exception e) {
			e.printStackTrace();
			
			
		}
		
		finally
		{
			try
			{
			if(os!=null)
				os.close();
			if(fis!=null)
				fis.close();
			}
			
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	

}
